﻿using IDEAMS.core;
using IDEAMS.core.opm;
using IDEAMS.model.alarm;
using IDEAMS.model.device;
using IDEAMS.model.result;
using IDEAMS.utils;
using System;
using System.IO;
using System.Windows.Media;

namespace IDEAMS.model
{
    public class DeviceInfo
    {
        /// <summary>
        /// 设备序列号
        /// </summary>
        public string serialNo { get; set; }

        /// <summary>
        /// 当前ip
        /// </summary>
        public string ip {  get; set; }

        /// <summary>
        /// 当前端口号
        /// </summary>
        public int port { get; set; }

        /// <summary>
        /// 背景色
        /// </summary>
        public Brush backgroundColor { get; set; }

        /// <summary>
        /// 检测结果
        /// </summary>
        public ProcessResult processResult { get; set; }

        /// <summary>
        /// 检测所在阶段
        /// </summary>
        public ProcessStep step { get; set; }

        /// <summary>
        /// 设备信息
        /// </summary>
        public DeviceParam deviceParam { get; set; }

        /// <summary>
        /// 光谱信息
        /// </summary>
        public SpectrumSummaryResp ssResp { get; set; }

        /// <summary>
        /// 报警记录
        /// </summary>
        public AlarmInfoCollection alarmInfoCollection { get; set; }

        /// <summary>
        /// 设备状态
        /// </summary>
        public DeviceStatus status { get; set; }

        /// <summary>
        /// 最长连续运行时长
        /// </summary>
        public double maxContinuousRunTime { get; set; }
        /// <summary>
        /// 本次运行时长
        /// </summary>
        public double continuousRunTime { get; set; }

        public DateTime testDate { get; set; }

        public int frequency { get; set; }

        public DeviceInfo()
        {
            processResult = new ProcessResult();
            step = ProcessStep.PRE_ETALON;
            deviceParam = new DeviceParam();
            ssResp = new SpectrumSummaryResp();
            alarmInfoCollection = new AlarmInfoCollection();
            status = DeviceStatus.NORMALI;
            maxContinuousRunTime = 0;
            continuousRunTime = 0;
            testDate = DateTime.Now;
            frequency = 0;
        }

        public DeviceInfo(DeviceInfo deviceInfo)
        {
            Copy(deviceInfo);
        }

        public DeviceInfo(DeviceInfoViewModel deviceInfo)
        {
            Copy(deviceInfo);
        }

        public void Copy(DeviceInfo deviceInfo)
        {
            serialNo = deviceInfo.serialNo;
            ip = deviceInfo.ip;
            port = deviceInfo.port;
            backgroundColor = deviceInfo.backgroundColor;

            step = deviceInfo.step;

            processResult = deviceInfo.processResult.Clone();

            deviceParam = deviceInfo.deviceParam.Clone();

            ssResp = deviceInfo.ssResp.Clone();

            alarmInfoCollection = deviceInfo.alarmInfoCollection.Clone();

            status = deviceInfo.status;
            maxContinuousRunTime = deviceInfo.maxContinuousRunTime;
            continuousRunTime = deviceInfo.continuousRunTime;
            testDate = deviceInfo.testDate;
            frequency = deviceInfo.frequency;
        }

        public void Copy(DeviceInfoViewModel deviceInfo)
        {
            serialNo = deviceInfo.serialNo;
            ip = deviceInfo.ip;
            port = deviceInfo.port;
            backgroundColor = deviceInfo.backgroundColor;

            step = deviceInfo.step;

            processResult = new ProcessResult(deviceInfo.processResult);

            deviceParam = new DeviceParam(deviceInfo.deviceParam);

            ssResp = new SpectrumSummaryResp(deviceInfo.ssResp);

            alarmInfoCollection = deviceInfo.alarmInfoCollection.Clone();

            status = deviceInfo.status;

            maxContinuousRunTime = deviceInfo.maxContinuousRunTime;
            continuousRunTime = deviceInfo.continuousRunTime;
            testDate = deviceInfo.testDate;
            frequency = deviceInfo.frequency;
        }

        public DeviceInfo Clone()
        {
            return new DeviceInfo(this);
        }

        public int Load(string paramSerialNo)
        {
            int rn = 0;
            string dataFile = string.Format(TW_App.fileManager.deviceInfoFile, paramSerialNo);
            bool existsFlag = File.Exists(dataFile);
            if (!existsFlag)
            {
                return -2;
            }

            DeviceInfo data = XmlUtil.Load<DeviceInfo>(dataFile);
            if(data != null)
            {
                Copy(data);
                rn = 0;
            }
            else
            {
                rn = -3;
            }
            return rn;
        }

        public bool Save(string paramSerialNo)
        {
            string dataFile = string.Format(TW_App.fileManager.deviceInfoFile, paramSerialNo);
            DeviceInfo data = new DeviceInfo(this);
            bool rn = XmlUtil.Save<DeviceInfo>(dataFile, data);
            return rn;
        }
    }
}
